<%--


--%>

<%@page contentType="text/html" pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Замещения</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <script type="text/javascript" src="resources/js/prototype.js"/>
    <script type="text/javascript" src="resources/js/goodwill.js"/>
    <script type="text/javascript" src="resources/js/shortcut.js"/>
</head>

<body>
<f:view>
<jsp:include page="menu.jsp"/>
<jsp:include page="status.jsp"/>
<h:form>

    <!-- Паджинатор -->
    <h:panelGroup layout="block">
        <h:panelGroup id="pager">

            <h:outputText value="Страница: #{ReplacementsBean.page}" styleClass="b"/>

            <a4j:commandLink action="#{ReplacementsBean.toBegin}" value="[<<" reRender="tblReplacements,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ReplacementsBean.longPrevPage}" value="<<" reRender="tblReplacements,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ReplacementsBean.prevPage}" value="<" reRender="tblReplacements,pager"
                             styleClass="pager_arr"/>


            <h:panelGroup id="pager_nums">
                <a4j:repeat value="#{ReplacementsBean.pageIndexes}" var="pageIndex">
                    <a4j:commandLink action="#{ReplacementsBean.applyPage}" value="#{pageIndex.num}"
                                     styleClass="pager_page" reRender="tblReplacements,pager">
                        <a4j:actionparam name="page_id" assignTo="#{ReplacementsBean.page}" value="#{pageIndex.num}"/>
                    </a4j:commandLink>
                </a4j:repeat>
            </h:panelGroup>

            <a4j:commandLink action="#{ReplacementsBean.nextPage}" value=">" reRender="tblReplacements,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ReplacementsBean.longNextPage}" value=">>" reRender="tblReplacements,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ReplacementsBean.toEnd}" value=">>]" reRender="tblReplacements,pager"
                             styleClass="pager_arr"/>

            <h:outputText value="(всего: #{ReplacementsBean.pagesCount})"/>
        </h:panelGroup>
        <!-- ---------- -->

        <rich:spacer width="10"/>

        <!-- Фильтр -->

        <h:outputText value="Фильтр: " styleClass="b"/>

        <h:outputText value="Кого замещает"/>
        <h:selectOneMenu value="#{SessionBean.replacementsFilter.managerId}" styleClass="f_s">
            <f:selectItems value="#{ReplacementsBean.replacementManager}"/>
        </h:selectOneMenu>

        <h:outputText value="Кто замещает"/>
        <h:selectOneMenu value="#{SessionBean.replacementsFilter.assistanceId}" styleClass="f_s">
            <f:selectItems value="#{ReplacementsBean.replacementAssistant}"/>
        </h:selectOneMenu>

        <h:outputText value="Дата замещения"/>
        <rich:calendar value="#{SessionBean.replacementsFilter.dateAsUtilDate}" locale="RU" datePattern="dd.MM.yyyy"
                       styleClass="f_tb_s"/>

        <a4j:commandButton reRender="tblReplacements,pager" value="Применить" action="#{ReplacementsBean.applyFilter}"/>
        <!-- ------ -->

    </h:panelGroup>

    <rich:dataTable value="#{ReplacementsBean.replacements}" var="replacement" id="tblReplacements" styleClass="full">
        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Замещения"/>
                <rich:spacer width="10"/>
                <a4j:commandLink action="#" reRender="addReplacementsPanel"
                                 oncomplete="#{rich:component('addReplacementsPanel')}.show()" id="bAddReplacements"
                                 styleClass="b">
                    <a4j:actionparam name="replacement_id" assignTo="#{ReplacementsBean.replacementId}" value="#{-1}"/>
                    <h:graphicImage value="/resources/img/add_16.png"/>
                    <h:outputText value="Добавить замещение" styleClass="fwhite"/>
                </a4j:commandLink>
                <rich:toolTip for="bAddReplacement" value="Добавить замещение" styleClass="hint"/>
            </h:panelGroup>
        </f:facet>
        <rich:column>

            <!-- Операции с замещениями --------------------------------------------------------------------------------------------------->

            <!-- Редактировать замещение -->
            <a4j:commandLink ajaxSingle="true" id="editlink" action="#"
                             oncomplete="#{rich:component('addReplacementsPanel')}.show()">
                <a4j:actionparam name="replacement_id" assignTo="#{ReplacementsBean.replacementId}"
                                 value="#{replacement.id}"/>
                <h:graphicImage value="/resources/img/edit.gif"/>
            </a4j:commandLink>
            <rich:toolTip for="editlink" value="Редактировать"/>

            <!-- Удалить замещение -->
            <a4j:commandLink ajaxSingle="true" id="deletelink"
                             oncomplete="#{rich:component('removeReplacementsPanel')}.show()">
                <a4j:actionparam name="replacement_id" assignTo="#{ReplacementsBean.replacementId}"
                                 value="#{replacement.id}"/>
                <h:graphicImage value="/resources/img/delete.gif"/>
            </a4j:commandLink>
            <rich:toolTip for="deletelink" value="Удалить"/>

            <!-- ---------------------------------------------------------------------------------------------------------------------->

        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Кого замещает"/>
            </f:facet>
            <a4j:commandLink action="#" ajaxSingle="true" oncomplete="go('users')">
                <h:outputText value="#{replacement.manager.fio}"/>
                <a4j:actionparam name="user_id" assignTo="#{SessionBean.usersFilter.id}"
                                 value="#{replacement.manager.id}"/>
                <a4j:actionparam name="user_client_id" assignTo="#{SessionBean.usersFilter.clientId}" value="-1"/>
                <a4j:actionparam name="user_fio" assignTo="#{SessionBean.usersFilter.fio}" value=""/>
                <a4j:actionparam name="user_login" assignTo="#{SessionBean.usersFilter.login}" value=""/>
                <a4j:actionparam name="user_email" assignTo="#{SessionBean.usersFilter.email}" value=""/>
                <a4j:actionparam name="user_parent" assignTo="#{SessionBean.usersFilter.parentManagerId}" value="-1"/>
                <a4j:actionparam name="user_role" assignTo="#{SessionBean.usersFilter.roleId}" value="-1"/>
            </a4j:commandLink>

        </rich:column>
        <rich:column>
            <f:facet name="header">
                <h:outputText value="Кто замещает"/>
            </f:facet>
            <a4j:commandLink action="#" ajaxSingle="true" oncomplete="go('users')">
                <h:outputText value="#{replacement.assistant.fio}"/>
                <a4j:actionparam name="user_id" assignTo="#{SessionBean.usersFilter.id}"
                                 value="#{replacement.assistant.id}"/>
                <a4j:actionparam name="user_client_id" assignTo="#{SessionBean.usersFilter.clientId}" value="-1"/>
                <a4j:actionparam name="user_fio" assignTo="#{SessionBean.usersFilter.fio}" value=""/>
                <a4j:actionparam name="user_login" assignTo="#{SessionBean.usersFilter.login}" value=""/>
                <a4j:actionparam name="user_email" assignTo="#{SessionBean.usersFilter.email}" value=""/>
                <a4j:actionparam name="user_parent" assignTo="#{SessionBean.usersFilter.parentManagerId}" value="-1"/>
                <a4j:actionparam name="user_role" assignTo="#{SessionBean.usersFilter.roleId}" value="-1"/>
            </a4j:commandLink>
        </rich:column>
        <rich:column>
            <f:facet name="header">
                <h:outputText value="Дата начала"/>
            </f:facet>
            <h:outputText value="#{replacement.begdateAsUtilDate}" converter="DateConverter"/>
        </rich:column>
        <rich:column>
            <f:facet name="header">
                <h:outputText value="Дата окончания"/>
            </f:facet>
            <h:outputText value="#{replacement.enddateAsUtilDate}" converter="DateConverter"/>
        </rich:column>
    </rich:dataTable>
</h:form>

<!-- ---------------------------------------------------------------------------------------------------------------------->

<rich:modalPanel id="addReplacementsPanel" autosized="true" width="550">
    <f:facet name="header">
        <h:outputText value="Введите данные о новом замещении"/>
    </f:facet>
    <f:facet name="controls">
        <h:graphicImage value="/resources/img/close.png" styleClass="hidelink"
                        onclick="#{rich:component('addReplacementPanel')}.hide();return false;"/>
    </f:facet>
    <h:form>
        <a4j:outputPanel ajaxRendered="true">

            <jsp:include page="messages.jsp"/>

            <h:inputHidden value="#{ReplacementsBean.replacementId}"/>
            <h:inputHidden value="#{ReplacementsBean.replacement.id}"/>

            <h:panelGrid columns="2" styleClass="full" columnClasses="lt,rt" cellspacing="0" cellpadding="0">

                <h:outputText value="Кого замещают" styleClass="b"/>
                <h:panelGroup>
                    <h:selectOneMenu value="#{ReplacementsBean.replacement.managerId}"
                                     rendered="#{ReplacementsBean.replacement.id == -1}">
                        <f:selectItems value="#{ReplacementsBean.usersList}"/>
                    </h:selectOneMenu>
                    <h:outputText value="#{ReplacementsBean.replacement.manager.fio}"
                                  rendered="#{ReplacementsBean.replacement.id != -1}"/>
                    <h:inputHidden value="#{ReplacementsBean.replacement.managerId}"
                                   rendered="#{ReplacementsBean.replacement.id != -1}"/>
                </h:panelGroup>

                <h:outputText value="Кто замещает" styleClass="b"/>
                <h:panelGroup>
                    <h:selectOneMenu value="#{ReplacementsBean.replacement.assistantId}"
                                     rendered="#{ReplacementsBean.replacement.id == -1}">
                        <f:selectItems value="#{ReplacementsBean.usersList}"/>
                    </h:selectOneMenu>
                    <h:outputText value="#{ReplacementsBean.replacement.assistant.fio}"
                                  rendered="#{ReplacementsBean.replacement.id != -1}"/>
                    <h:inputHidden value="#{ReplacementsBean.replacement.assistantId}"
                                   rendered="#{ReplacementsBean.replacement.id != -1}"/>
                </h:panelGroup>

                <h:outputText value="Дата начала" styleClass="b"/>
                <rich:calendar value="#{ReplacementsBean.replacement.begdateAsUtilDate}" locale="RU"
                               datePattern="dd.MM.yyyy" style="width:200px"/>

                <h:outputText value="Дата завершения" styleClass="b"/>
                <rich:calendar value="#{ReplacementsBean.replacement.enddateAsUtilDate}" locale="RU"
                               datePattern="dd.MM.yyyy" style="width:200px"/>

            </h:panelGrid>

            <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellspacing="0" cellpadding="0">
                <a4j:commandButton value="Сохранить"
                                   action="#{ReplacementsBean.addReplacement}"
                                   reRender="tblReplacements,pager"
                                   styleClass="btnsz"
                                   oncomplete="if(#{!ReplacementsBean.hasFacesErrors}) #{rich:component('addReplacementsPanel')}.hide();">
                </a4j:commandButton>
                <a4j:commandButton value="Отменить"
                                   styleClass="btnsz"
                                   onclick="#{rich:component('addReplacementsPanel')}.hide();return false;"/>
            </h:panelGrid>

        </a4j:outputPanel>
    </h:form>
</rich:modalPanel>

<rich:modalPanel id="removeReplacementsPanel" autosized="true" width="500">
    <f:facet name="header">
        <h:outputText value="Внимание!"/>
    </f:facet>
    <f:facet name="controls">
        <h:graphicImage value="/resources/img/close.png" styleClass="hidelink"
                        onclick="#{rich:component('removeReplacementsPanel')}.hide();return false;"/>
    </f:facet>
    <a4j:outputPanel ajaxRendered="true">
        <h:form>
            <jsp:include page="messages.jsp"/>

            <h:inputHidden value="#{ReplacementsBean.replacementId}"/>
            <h:inputHidden value="#{ReplacementsBean.replacement.id}"/>

            <h:panelGrid columns="2" cellpadding="0" cellspacing="0" columnClasses="top,top">
                <h:graphicImage value="/resources/img/warning_32.png"/>
                <h:panelGroup>
                    <h:outputText value="Удалить замещение?"/>
                    <h:panelGrid columns="2" cellpadding="0" cellspacing="0" columnClasses="lt,rt">
                        <h:outputText value="Замещаемый менеджер" styleClass="b"/>
                        <h:outputText value="#{ReplacementsBean.replacement.manager.fio}"/>
                        <h:outputText value="Замещающий менеджер" styleClass="b"/>
                        <h:outputText value="#{ReplacementsBean.replacement.assistant.fio}"/>
                        <h:outputText value="Дата начала замещения" styleClass="b"/>
                        <h:outputText
                                value="#{ReplacementsBean.replacement.begdateAsUtilDate == null ? 'не установлена' : ReplacementsBean.replacement.begdateAsUtilDate}"/>
                        <h:outputText value="Дата окончания замещения" styleClass="b"/>
                        <h:outputText
                                value="#{ReplacementsBean.replacement.enddateAsUtilDate == null ? 'не установлена' : ReplacementsBean.replacement.enddateAsUtilDate}"/>
                    </h:panelGrid>
                </h:panelGroup>
            </h:panelGrid>

            <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellspacing="0" cellpadding="0">
                <a4j:commandButton value="Да"
                                   action="#{ReplacementsBean.removeReplacement}"
                                   reRender="tblReplacements,pager"
                                   styleClass="btnsz"
                                   oncomplete="if(#{!ReplacementsBean.hasFacesErrors}) #{rich:component('removeReplacementsPanel')}.hide();"/>
                <a4j:commandButton value="Нет"
                                   styleClass="btnsz"
                                   onclick="#{rich:component('removeReplacementsPanel')}.hide();return false;"/>
            </h:panelGrid>
        </h:form>
    </a4j:outputPanel>
</rich:modalPanel>

</f:view>
</body>
</html>
